<template>
  <div class="rated">
    <!-- 上面的最受欢迎列表组件 -->
    <p class="title">最受好评电影</p>
    <div class="wrapper" style="overflow: hidden">
      <ul>
        <li v-for="item in ratedList" :key="item._id">
          <div class="img-wrap">
            <img :src="item.imgUrl" alt="" />
            <div class="shadow" v-if="item.score">
              观众评分: {{ item.score }}
            </div>
            <div class="shadow" v-else>{{ item.wishNum }}人想看</div>
          </div>

          <h5>{{ item.title }}</h5>
        </li>
      </ul>
    </div>
  </div>
</template>

<script>
// 引入BetterScroll，用于做元素的滚动
import BetterScroll from "better-scroll";
import { mapState, mapActions } from "vuex";

export default {
  computed: {
    // ...mapState(["ratedList"]),
    ...mapState({
      ratedList: (state) => state.home.ratedList,
    }),
  },
  async mounted() {
    await this.getRatedAsync();
    new BetterScroll(".wrapper", {
      startX: 0,
      scrollX: true,
      scrollY: false,
      // 保留子元素的点击事件
      click: true,
    });

    // 必须是数据请求完毕以后才能添加
  },
  methods: {
    ...mapActions("home", ["getRatedAsync"]),
  },
};
</script>

<style lang="less" scoped>
.rated {
  width: 100%;
  height: 205px;
  background: #fff;
  padding: 12px 15px;

  .title {
    font-size: 14px;
    color: #333;
    margin: 0 0 12px;
  }

  ul {
    // width: 800px;
    // display: inline;
    height: 150px;
    // 转成行内的弹性盒
    display: inline-flex;
    // overflow: auto;
    // 已经用了bs，不需要滚动条
    overflow: hidden;

    li {
      width: 85px;
      height: 145px;
      flex-shrink: 0;
      margin-right: 10px;

      .img-wrap {
        width: 85px;
        height: 115px;
        margin-bottom: 5px;
        position: relative;

        img {
          width: 100%;
          height: 100%;
        }

        .shadow {
          position: absolute;
          width: 100%;
          height: 24px;
          left: 0;
          bottom: 0;
          background: linear-gradient(
            to bottom,
            rgba(0, 0, 0, 0.2),
            rgba(0, 0, 0, 0.7)
          );
          font-size: @xs-font;
          color: #faaf00;
          line-height: 30px;
        }
      }

      h5 {
        font-size: @s-font;
        width: 100%;
        .ell();
      }

      &:last-child {
        margin: 0;
      }
    }
  }
}
</style>
